﻿using System;
using System.Drawing;
using Microsoft.WindowsAPICodePack.Taskbar;
using Slippor.Douban.Radio.Assist;
using Slippor.Douban.Radio.BaseForm;
using Slippor.Douban.Radio.Properties;

namespace Slippor.Douban.Radio
{
    public class Main : DoubanForm
    {
        protected override void OnHeightChanged(int height)
        {
            _infoRect = new Rectangle(_infoRect.X, _infoRect.Y + height, _infoRect.Width, _infoRect.Height);
            _coverRect = new Rectangle(_coverRect.X, _coverRect.Y + height, _coverRect.Width, _coverRect.Height);
            _currentRect = _settings.SnapShot == "Info" ? _infoRect : _coverRect;
            if (TaskbarManager.IsPlatformSupported && ShowInTaskbar)
            {
                TaskbarManager.Instance.TabbedThumbnail.SetThumbnailClip(Handle, _currentRect);
            }
        }

        protected override void InitialFields()
        {
            CurrentVersion.VersionMark = "W7";
        }

        protected override void InitialControls()
        {
            base.InitialControls();
            InitialThumbnailButtons();
        }

        private void InitialThumbnailButtons()
        {
            _btnControl.Click += btn_Click;
            _btnDel.Click += btn_Click;
            _btnFav.Click += btn_Click;
            _btnNext.Click += btn_Click;
            _btnSearch.Click += _btnSearch_Click;
            _btnNotify.Click += _btnNotify_Click;
            _btnSwitch.Click += _btnSwitch_Click;
        }

        protected override void OnRadioLoaded()
        {
            if (TaskbarManager.IsPlatformSupported && ShowInTaskbar)
            {
                TaskbarManager.Instance.TabbedThumbnail.SetThumbnailClip(Handle, _currentRect);
            }
        }

        protected override void OnSetFav(Song song)
        {
            base.OnSetFav(song);
            switch (song.FaveState)
            {
                case FavState.Fav:
                    UpdateButton(_btnFav, ImageCache.Fav, null, "取消", true);
                    break;
                case FavState.UnFav:
                    UpdateButton(_btnFav, ImageCache.UnFav, null, "我喜欢", true);
                    break;
                case FavState.Unknown:
                    break;
            }
        }

        protected override void OnSetType(RadioType radioType)
        {
            base.OnSetType(radioType);
            if (TaskbarManager.IsPlatformSupported && ShowInTaskbar)
            {
                switch (radioType)
                {
                    case RadioType.PrivateRadio:
                        _btnDel.Enabled = _isPlaying;
                        break;
                    case RadioType.PublicRadio:
                        _btnDel.Enabled = false;
                        break;
                    case RadioType.Unknown:
                        _btnDel.Enabled = _isPlaying;
                        break;
                }
            }
        }

        protected override void OnExecute(Command command)
        {
            base.OnExecute(command);
            switch (command)
            {
                case Command.Control:
                    ConfirmTaskBar();
                    break;
            }
        }

        protected override void InitialProperties()
        {
            base.InitialProperties();
            _settings.Upgrade();
        }

        protected override void ShowTaskBar()
        {
            base.ShowTaskBar();
            ConfirmTaskBar();
        }

        protected override void HideTaskBar()
        {
            base.HideTaskBar();
            _taskBarBuilt = false;
        }

        #region 任务栏

        private readonly ThumbnailToolbarButton _btnControl = new ThumbnailToolbarButton(ImageCache.Shut, "停止");
        private readonly ThumbnailToolbarButton _btnDel = new ThumbnailToolbarButton(Resources.Del, "不再播放");
        private readonly ThumbnailToolbarButton _btnFav = new ThumbnailToolbarButton(ImageCache.UnFav, "我喜欢");
        private readonly ThumbnailToolbarButton _btnNext = new ThumbnailToolbarButton(Resources.Next, "跳过曲目");
        private readonly ThumbnailToolbarButton _btnNotify = new ThumbnailToolbarButton(Resources.Notify, "隐藏到托盘");
        private readonly ThumbnailToolbarButton _btnSearch = new ThumbnailToolbarButton(Resources.Search, "搜索");
        private readonly ThumbnailToolbarButton _btnSwitch = new ThumbnailToolbarButton(ImageCache.Info, "显示信息");
        private Rectangle _coverRect = new Rectangle(28, 34, 120, 120);
        private Rectangle _infoRect = new Rectangle(180, 56, 215, 42);
        private readonly Settings _settings = new Settings();
        private Rectangle _currentRect;
        private bool _taskBarBuilt;

        private void ConfirmTaskBar()
        {
            if (TaskbarManager.IsPlatformSupported && ShowInTaskbar)
            {
                if (!_taskBarBuilt)
                {
                    CreateTaskbar();
                    CreateJumpList();
                    _taskBarBuilt = true;
                    if (_settings.SnapShot == "Info")
                    {
                        UpdateButton(_btnSwitch, ImageCache.Cover, null, "显示封面", false);
                    }
                    else
                    {
                        UpdateButton(_btnSwitch, ImageCache.Info, null, "显示信息", false);
                    }
                }

                if (_isPlaying)
                {
                    UpdateButton(_btnControl, ImageCache.Pause, ImageCache.Play, "暂停", true);
                }
                else
                {
                    UpdateButton(_btnControl, ImageCache.Play, ImageCache.Pause, "播放", true);
                }
                _btnNext.Enabled = _isPlaying;
                _btnFav.Enabled = _isPlaying;
                _btnDel.Enabled = _isPlaying && (_currentType != RadioType.PublicRadio);
                _btnSearch.Enabled = _isPlaying;
            }
        }

        private void CreateTaskbar()
        {
            TaskbarManager.Instance.ThumbnailToolbars.AddButtons(Handle, _btnFav, _btnDel, _btnNext, _btnControl,_btnSearch, _btnSwitch, _btnNotify);
            if (_settings.SnapShot == "Info")
            {
                _currentRect = _infoRect;
            }
            else
            {
                _currentRect = _coverRect;
            }
            TaskbarManager.Instance.TabbedThumbnail.SetThumbnailClip(Handle, _currentRect);
        }

        private void CreateJumpList()
        {
            JumpList jumpList = JumpList.CreateJumpList();
            jumpList.AddUserTasks(
                new JumpListSeparator(),
                new JumpListLink("http://douban.com/", "豆瓣"),
                new JumpListLink("http://douban.fm/radio", "豆瓣电台"),
                new JumpListLink("http://douban.fm/", "豆瓣电台设置"));
            jumpList.Refresh();
        }

        private void UpdateButton(ThumbnailToolbarButton button, Icon toolBarIcon, Icon overlayIcon, String toolTip, bool updateOverlay)
        {
            if (TaskbarManager.IsPlatformSupported && ShowInTaskbar)
            {
                if (_taskBarBuilt)
                {
                    button.Icon = toolBarIcon;
                    button.Tooltip = toolTip;
                    if (updateOverlay)
                    {
                        if (overlayIcon != null)
                        {
                            TaskbarManager.Instance.SetOverlayIcon(Handle, overlayIcon, toolTip);
                        }
                        else
                        {
                            TaskbarManager.Instance.SetOverlayIcon(Handle, toolBarIcon, toolTip);
                        }
                    }
                }
            }
        }

        private void _btnSearch_Click(object sender, ThumbnailButtonClickedEventArgs e)
        {
            DefaultSearch.Search(_currentSong.Title);
        }

        private void _btnSwitch_Click(object sender, ThumbnailButtonClickedEventArgs e)
        {
            if (_currentRect == _infoRect)
            {
                _currentRect = _coverRect;
                UpdateButton(_btnSwitch, ImageCache.Info, null, "显示信息", false);
                _settings.SnapShot = "Cover";
            }
            else
            {
                _currentRect = _infoRect;
                UpdateButton(_btnSwitch, ImageCache.Cover, null, "显示封面", false);
                _settings.SnapShot = "Info";
            }
            _settings.Save();
            if (TaskbarManager.IsPlatformSupported && ShowInTaskbar)
            {
                TaskbarManager.Instance.TabbedThumbnail.SetThumbnailClip(Handle, _currentRect);
            }
        }

        private void _btnNotify_Click(object sender, ThumbnailButtonClickedEventArgs e)
        {
            HideToNotify();
        }

        private void btn_Click(object sender, EventArgs e)
        {
            var btn = (ThumbnailToolbarButton) sender;
            ICommand command = CommandFactory.GetCommand(Command.Other, _currentType);
            if (btn == _btnFav)
            {
                command = CommandFactory.GetCommand(Command.Fav, _currentType);
            }
            else if (btn == _btnDel)
            {
                command = CommandFactory.GetCommand(Command.Del, _currentType);
            }
            else if (btn == _btnNext)
            {
                command = CommandFactory.GetCommand(Command.Next, _currentType);
            }
            else if (btn == _btnControl)
            {
                command = CommandFactory.GetCommand(Command.Control, _currentType);
            }
            ExcuteCommand(command);
        }

        #endregion

        #region EventHandle

        protected override void OnDisplayed()
        {
            base.OnDisplayed();
            ConfirmTaskBar();
        }

        protected override void OnClosed()
        {
            if (TaskbarManager.IsPlatformSupported && ShowInTaskbar)
            {
                TabbedThumbnail preview = TaskbarManager.Instance.TabbedThumbnail.GetThumbnailPreview(this);
                if (preview != null)
                {
                    TaskbarManager.Instance.TabbedThumbnail.RemoveThumbnailPreview(preview);
                }
            }
        }

        protected override void OnResetting()
        {
            base.OnResetting();
            ConfirmTaskBar();
        }

        #endregion
    }
}